package com.hoperun.product.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hoperun.distributecloth.R;


/**
 * Created by Administrator on 2019/8/22 0022.
 */

public class BaseUtil {
    //dp转px
    public static int getPixelsFromDp(Activity context, int size) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        return (size * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
    }
    //设置背景透明度
    public static void setBackgroundAlpha(Activity context,float alpha) {
        WindowManager.LayoutParams layoutParams = context.getWindow().getAttributes();
        layoutParams.alpha = alpha;
        context.getWindow().setAttributes(layoutParams);
    }
    //dialog弹出框公共方法
    public static AlertDialog ShowDialog(Activity context,View viewTip,int widthdp){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.dialog);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);;
        alertDialog.show();
        alertDialog.getWindow().setContentView(viewTip);
        if(widthdp>0){
            WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
            layoutParams.width =getPixelsFromDp(context,widthdp);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            alertDialog.getWindow().setAttributes(layoutParams);
        }
        return  alertDialog;
    }

    public static Dialog ShowMYDialog(Activity context, View viewTip, int widthdp){
        Dialog alertDialog= new Dialog(context,R.style.dialog);
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);;
        alertDialog.show();
        alertDialog.getWindow().setContentView(viewTip);
        if(widthdp>0){
            WindowManager.LayoutParams layoutParams = alertDialog.getWindow().getAttributes();
            layoutParams.width =getPixelsFromDp(context,widthdp);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            alertDialog.getWindow().setAttributes(layoutParams);
        }
        return  alertDialog;
    }



//    //确定按钮提示弹出框
//    public static void  ShowMessageDialog(Activity activity,String content){
//        View viewTips = LayoutInflater.from(activity).inflate(R.layout.common_warn_alert, null);
//        final  AlertDialog alertDialog=ShowDialog(activity,viewTips,300);
//        viewTips.findViewById(R.id.cancelbtn).setVisibility(View.GONE);
//        viewTips.findViewById(R.id.shuxian).setVisibility(View.GONE);
//        Button shurebuttons= viewTips.findViewById(R.id.shuerbtn);
//        shurebuttons.setText("确定");
//        TextView textView= viewTips.findViewById(R.id.content);
//        textView.setText(content);
//        alertDialog.show();
//        shurebuttons.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                alertDialog.dismiss();
//            }
//       });
//    }


    /**
     * 通过SnackBar现在的背景,获取其设置圆角值时候所需的GradientDrawable实例
     * @param backgroundOri
     * @return
     */
    public static GradientDrawable getRadiusDrawable(Drawable backgroundOri) {
        GradientDrawable background = null;
        if (backgroundOri instanceof GradientDrawable) {
            background = (GradientDrawable) backgroundOri;
        } else if (backgroundOri instanceof ColorDrawable) {
            int backgroundColor = ((ColorDrawable) backgroundOri).getColor();
            background = new GradientDrawable();
            background.setColor(backgroundColor);
        } else {
        }
        return background;
    }


}
